//
//  LSZDynamicOtherViewController+LCCycle.m
//  JKFuYinProject
//
//  Created by admin on 2018/4/12.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZDynamicOtherViewController+LCCycle.h"

@implementation LSZDynamicOtherViewController (LCCycle)


- (UIView *)setupCycleWithCycleHeight:(CGFloat)cycleHeight {
    [self addPagerViewWithCycleHeight:cycleHeight];
    [self addPageControl];
    return self.pagerView;
}
- (UIView *)addPagerViewWithCycleHeight:(CGFloat)cycleHeight {
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc] init];
    self.pagerView = pagerView;
    pagerView.isInfiniteLoop = YES;
    pagerView.autoScrollInterval = 5.0;
    pagerView.dataSource = self;
    pagerView.delegate = self;

    self.pagerView.frame = CGRectMake(0, 0, ScreenWidth, cycleHeight);
    [self.pagerView.layer setMasksToBounds:YES];
    [self.pagerView.layer setCornerRadius:4];
    self.pagerView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.95 alpha:1.00];
//    pagerView.backgroundColor = [UIColor yellowColor];

    [pagerView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:@"cellId"];

    return self.pagerView;
}
- (void)addPageControl {
    TYPageControl *pageControl = [[TYPageControl alloc]init];
    self.pageControl = pageControl;
    self.pageControl.frame = CGRectMake(0, CGRectGetHeight(self.pagerView.frame) - 26, CGRectGetWidth(self.pagerView.frame), 26);
    pageControl.currentPageIndicatorSize = CGSizeMake(8, 8);
    //    pageControl.pageIndicatorImage = [UIImage imageNamed:@"Dot"];
    //    pageControl.currentPageIndicatorImage = [UIImage imageNamed:@"DotSelected"];
    //    pageControl.contentInset = UIEdgeInsetsMake(0, 20, 0, 20);
    //    pageControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    //    pageControl.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //    [pageControl addTarget:self action:@selector(pageControlValueChangeAction:) forControlEvents:UIControlEventValueChanged];
    [self.pagerView addSubview:pageControl];
}
#pragma mark - TYCyclePagerViewDelegate
///pagerView did selected item cell
- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(UICollectionViewCell *)cell atIndex:(NSInteger)index{
    if (self.images == nil) {
        return;
    }
    [self didSelectItemAtIndex:index];
    NSLog(@"index = %ld", (long)index);
}

#pragma mark - 轮播图的点击事件
- (void)didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"index = %ld", (long)index);
//    LSZNewAudioPlayImageAndTableViewVC * vc = [[LSZNewAudioPlayImageAndTableViewVC alloc]init];
//    vc.strTopImageView = self.cycleArray[index].image;
//    vc.strLastImageView = self.cycleArray[index].image;
//    vc.titleStr = self.cycleArray[index].singerName;
//    vc.type = @"zhuanji";
//    vc.typeStr = @"zhuanji";
//    vc.parentId = self.cycleArray[index].id;
//    vc.typeComeWhere = @"1";
//    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - TYCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    if (self.images == nil) {
        self.pagerView.hidden = YES;
        return 0;
    }
    self.pagerView.hidden = NO;
    return self.images.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    TYCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    if (self.images.count > index) {
        CGFloat height = [LSZSpecialPackaging initWithReturnsthesizeOf:210 :10];
        CGRect rect = cell.imageViewBack.frame;
        rect.size.width= self.view.frame.size.width;
        rect.size.height = height;
        cell.imageViewBack.frame = rect;
        NSLog(@"%@>>>>",self.images[index]);
        NSString * State = NullStringjudgment([[NSUserDefaults standardUserDefaults]objectForKey:@"headerPhoto"]);
        [cell.imageViewBack sd_setImageWithURL:[NSURL URLWithString:State] placeholderImage:[UIImage imageNamed:@"devotion_bannerA"]];
        //self.images[index]
//        [cell.imageViewBack yy_setImageWithURL:[NSURL URLWithString:State] placeholder:[UIImage imageNamed:@"devotion_bannerA"]];
    }
    return cell;
}
- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)/***0.92*/, CGRectGetHeight(pageView.frame)/***0.945*/);
    layout.itemSpacing = 0;//5.5;
    layout.itemHorizontalCenter = YES;
    layout.layoutType = 1;
    layout.minimumScale = 1;//0.905;
    return layout;
}
- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    self.pageControl.currentPage = toIndex;
    //[_pageControl setCurrentPage:newIndex animate:YES];
    //    NSLog(@"%zd ->  %zd",fromIndex,toIndex);
}
-(TYPageControl *)pageControl{
    return objc_getAssociatedObject(self, &pageControlKey);
}
-(void)setPageControl:(TYPageControl *)pageControl{
    objc_setAssociatedObject(self, &pageControlKey, pageControl, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(void)loadDataWithPage:(BOOL)page{
    NSLog(@"%@>>>>>",self.images);
//    self.cycleArray = self.images;
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self loadCycleScrollViewData];
    }];
}
-(void)loadCycleScrollViewData{
    self.pageControl.numberOfPages = self.images.count;
    [self.pagerView reloadData];
}

@end
